package com.mxpio.erp.sales.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.enums.OrderEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.jpa.model.OrderLine;
import com.mxpio.erp.common.jpa.model.OrderLineKey;
import com.mxpio.erp.common.order.service.OrderLineService;
import com.mxpio.erp.common.sales.entity.RejectBill;
import com.mxpio.erp.common.sales.entity.RejectBillLine;
import com.mxpio.erp.common.sales.entity.SalesOrderLine;
import com.mxpio.erp.common.sales.entity.ShippingNotice;
import com.mxpio.erp.common.sales.entity.ShippingNoticeLine;
import com.mxpio.erp.common.sales.service.SalesOrderLineService;
import com.mxpio.erp.common.vo.NoticeClassVo;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "SalesOrderLineController", description = "销售订单行接口")
@RestController
@RequestMapping("/erp/sales/soline/")
public class SalesOrderLineController {

    @Autowired
    private SalesOrderLineService salesOrderLineService;

    @Autowired
    private OrderLineService orderLineService;
    
    @GetMapping("list")
    @Operation(summary = "销售订单行列表", description = "获取销售行订单列表（无分页）", method = "GET")
    public Result<List<SalesOrderLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<SalesOrderLine> salesOrderLines = salesOrderLineService.list(criteria);
        return Result.OK(salesOrderLines);
    }

    @GetMapping("auditedList")
    @Operation(summary = "销售订单行列表(已审批)", description = "获取销售行订单列表（无分页）(已审批)", method = "GET")
    public Result<List<SalesOrderLine>> auditedList(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<SalesOrderLine> salesOrderLines = salesOrderLineService.auditList(criteria);
        return Result.OK(salesOrderLines);
    }

    @GetMapping("page")
    @Operation(summary = "销售订单行列表", description = "获取销售订单行列表（分页）", method = "GET")
    public Result<Page<SalesOrderLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<SalesOrderLine> salesOrderLines = salesOrderLineService.listPage(page, criteria);
        NoticeClassVo shippingNoticeClazz = new NoticeClassVo(ShippingNotice.class,ShippingNoticeLine.class);
        NoticeClassVo rejectBillNoticeClazz = new NoticeClassVo(RejectBill.class, RejectBillLine.class);
        List<NoticeClassVo> noticeClassVos = new ArrayList<NoticeClassVo>();
        noticeClassVos.add(shippingNoticeClazz);
        noticeClassVos.add(rejectBillNoticeClazz);
        orderLineService.handleBizOrderLineQuantity(salesOrderLines.getContent(), noticeClassVos, OrderEnums.AccessType.OUT.getCode());
        return Result.OK(salesOrderLines);
    }
    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取销售订单行", description = "根据bizNo获取销售订单行", method = "GET")
    public Result<List<SalesOrderLine>> listByBizNo(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<SalesOrderLine> salesOrderLine = salesOrderLineService.list(c);
        return Result.OK(salesOrderLine);
    }

    @GetMapping("list/{bizNo}/{lineNo}")
    @Operation(summary = "根据bizNo获取销售订单行", description = "根据bizNo获取销售订单行", method = "GET")
    public Result<SalesOrderLine> getById(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo,@Parameter(description="行号") @PathVariable(name = "lineNo", required = true)String lineNo) {
        OrderLineKey lineKey = new OrderLineKey();
        lineKey.setLineNo(lineNo);
        lineKey.setBizNo(bizNo);
        SalesOrderLine salesOrderLine = salesOrderLineService.getById(lineKey);
        return Result.OK(salesOrderLine);
    }

    @PostMapping("add")
    @Operation(summary = "新增销售订单行", description = "新增销售订单行", method = "POST")
    public Result<SalesOrderLine> add(@RequestBody SalesOrderLine salesOrderLine) {
        salesOrderLineService.save(salesOrderLine);
        return Result.OK(salesOrderLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑销售订单行", description = "编辑销售订单行", method = "PUT")
    public Result<SalesOrderLine> edit(@RequestBody SalesOrderLine salesOrderLine) {
        salesOrderLineService.update(salesOrderLine);
        return Result.OK(salesOrderLine);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除销售订单行", description = "删除销售订单行,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", method = "DELETE")
    public Result<SalesOrderLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            String[] pk = key.split(":");
            OrderLineKey lineKey = new OrderLineKey();
            lineKey.setBizNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            salesOrderLineService.remove(SalesOrderLine.class, lineKey);
        }
        return Result.OK();
    }

    @PutMapping("open/{ids}")
    @Operation(summary = "打开销售订单行", description = "打开销售订单行,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", method = "PUT")
    public Result<? extends OrderLine> open(@PathVariable(name = "ids", required = true) String ids) {
        return orderLineService.updateCloseStatus(SalesOrderLine.class, ids, OrderStatusEnums.CloseStatus.OPEN);
    }

    @PutMapping("close/{ids}")
    @Operation(summary = "关闭销售订单行", description = "关闭销售订单行", method = "PUT")
    public Result<? extends OrderLine> close(@Parameter(description="业务单号，多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后") @PathVariable(name = "ids", required = true) String ids) {
        return orderLineService.updateCloseStatus(SalesOrderLine.class, ids, OrderStatusEnums.CloseStatus.CLOSED);
    }

}
